# -*- coding: utf-8 -*-

from PyQt5.QtCore import( 
    Qt, 
    QPointF,
    QTimer
)
from PyQt5.QtWidgets import (
    QAction
)

from PyQt5.QtGui import (
    QTransform,
    QColor
)

from qgis.gui import (
    QgsMapMouseEvent, 
    QgsMapToolEmitPoint,
    QgsRubberBand
)

from qgis.core import(
    QgsWkbTypes,
    QgsGeometry,
    QgsApplication,
    QgsProject,
    QgsPointXY
) 

import math

class MoveGeometryTool(QgsMapToolEmitPoint):
    def __init__(self, canvas,rubber_band:QgsRubberBand):
        QgsMapToolEmitPoint.__init__(self, canvas)
        self.__canvas = canvas
        self.__crs = canvas.mapSettings().destinationCrs()

        self.__rubber_band = rubber_band
        self.__bf_geometry = rubber_band.asGeometry()
        self.__is_move = False
        self.__move_point = None

    def activate(self):
        QgsMapToolEmitPoint.activate(self)
        self.__canvas.setCursor(Qt.CrossCursor)

    def deactivate(self):
        QgsMapToolEmitPoint.deactivate(self)


    def canvasPressEvent(self, event):
        point = self.toMapCoordinates(event.pos())
        # 构造一个点的QgsGeometry对象
        point_geom = QgsGeometry.fromPointXY(point)
        if event.button() == Qt.LeftButton:
            geometry = self.__rubber_band.asGeometry()
            if geometry.contains(point_geom):
                self.__is_move = True
                self.__move_point = point
                self.__bf_geometry = self.__rubber_band.asGeometry()
        elif event.button() == Qt.RightButton:
            if self.__is_move==True :
                self.__rubber_band.reset(QgsWkbTypes.PolygonGeometry)
                self.__rubber_band.setToGeometry(self.__bf_geometry,self.__crs)
                    
                self.__is_move = False
                self.__move_point = None
                self.__bf_geometry = None


    def canvasMoveEvent(self, event):
        if self.__is_move==True:
            
            geometry =  QgsGeometry(self.__bf_geometry)

            point = self.toMapCoordinates(event.pos())
            # print(point.x() - self.__move_point.x(),
            #       point.y()- self.__move_point.y())
            geometry.translate(point.x() - self.__move_point.x(),
                               point.y() - self.__move_point.y())
            
            self.__rubber_band.reset(QgsWkbTypes.PolygonGeometry)
            self.__rubber_band.setToGeometry(geometry,self.__crs)

    def canvasReleaseEvent(self, event):
        if event.button() == Qt.LeftButton:
            if self.__is_move==True:
                geometry = QgsGeometry(self.__bf_geometry)

                point = self.toMapCoordinates(event.pos())
                geometry.translate(point.x() - self.__move_point.x(),
                                   point.y() - self.__move_point.y())
                
                self.__rubber_band.reset(QgsWkbTypes.PolygonGeometry)
                self.__rubber_band.setToGeometry(geometry,self.__crs)
                
            self.__is_move = False
            self.__move_point = None
            self.__bf_geometry = None
            
    
        

